package sevencm.wechat.data;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import sevencm.wechat.common.DynamicDataSource;

import javax.sql.DataSource;


@Configuration
public class DataSourceConfig {

  @Value("${manifest.db.path}")
  private String manifestDBPath;

  private static final String NAME = "Manifest.db";

  @Bean
  public DataSource defaultDataSource() {
    DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
    dataSourceBuilder.driverClassName("org.sqlite.JDBC");
    dataSourceBuilder.url("jdbc:sqlite:"+manifestDBPath+NAME);
    return dataSourceBuilder.build();
  }


  @Bean
  @Primary
  @DependsOn({"springUtils", "defaultDataSource"})
  public DynamicDataSource dataSource() {
    DynamicDataSource dynamicDataSource = new DynamicDataSource();
    dynamicDataSource.setTargetDataSources(DynamicDataSource.dataSourcesMap);
    return dynamicDataSource;
  }
  
}